var randomUtils = require ('./randomUtils');
var configUtils = require ('./configUtils');
var http$ = configUtils.getConfig ('log', 'http', false);
module.exports = {
  getRateX (x, w) {
    return parseInt (x * (device.width / (w || device.width)));
  },
  getRateY (y, h) {
    return parseInt (y * (device.height / (h || device.height)));
  },
  clickXY (x, y) {
    var showing = console.showing;
    if (http$) {
      console.hide ();
      sleep (randomUtils.getRandom (100, 1000));
    }
    click (x, y);
    if (showing) {
      console.show ();
    }
  },
  clickXYZW (x, y, z, w, deltaX, deltaY) {
    deltaX = deltaX || 1;
    deltaY = deltaY || 1;
    this.clickXY (
      random (x + deltaX, z - deltaX),
      random (y + deltaY, w - deltaY)
    );
  },
  clickBounds (bounds, deltaX, deltaY) {
    var left = bounds.left;
    var right = bounds.right;
    var top = bounds.top;
    var bottom = bounds.bottom;
    this.clickXYZW (left, top, right, bottom, deltaX, deltaY);
  },
  clickPointMap (pointMap, deltaX, deltaY) {
    var point = pointMap[0];
    var width = pointMap[1];
    var height = pointMap[2];
    var x = this.getRateX (point[0], width);
    var y = this.getRateY (point[1], height);
    var z = this.getRateX (point[2], width);
    var w = this.getRateY (point[3], height);
    this.clickXYZW (x, y, z, w, deltaX, deltaY);
  },
  clickTextUI (TextUI, deltaX, deltaY) {
    this.clickBounds (TextUI.bounds (), deltaX, deltaY);
  },
  clickTextOCR (TextOCR, deltaX, deltaY) {
    this.clickBounds (TextOCR.bounds, deltaX, deltaY);
  },
};
